﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Media;

namespace SCOSub
{
    class GetInfo_staff :IGetInfo 
    {
        [DllImport("user32.dll", EntryPoint = "FindWindow")]
        private static extern int FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(int hwnd, int wMsg, int wParam, StringBuilder lParam);

        private MainForm mainform;
        public GetInfo_staff(MainForm mainform)
        {
            this.mainform = mainform;
        }
        public  bool getTimeTip_staff(out string timetip)//getinfo--staff mode
        {
            int hwnd1 = FindWindow("VirtualDub", null);
            if (hwnd1 == 0)
            {
                SystemSounds.Hand.Play();
                timetip = "+00000000";
                mainform.updataStatue(Errors.FailtoFindVDM);
                return false;
            }
            int hwnd2 = FindWindowEx(hwnd1, 0, "birdyPositionControl", null);
            if (hwnd2 == 0)
            {
                SystemSounds.Hand.Play();
                timetip = "+00000000";
                mainform.updataStatue(Errors.FailtoFindVDM);
                return false;
                
            }
            int hwnd = FindWindowEx(hwnd2, 0, "Edit", null);
            StringBuilder getString = new StringBuilder(30);
            SendMessage(hwnd, 0x000D, 100, getString);
            Regex analyses = new Regex("([0-9]+)");
            Match m = analyses.Match(getString.ToString());
            try
            {
                timetip = string.Format("+{0:D8}", int.Parse(m.Groups[0].Captures[0].Value));
                return true;
            }
            catch
            {
                SystemSounds.Hand.Play();
                timetip = "+00000000";
                mainform.updataStatue(Errors.VDMNoFileOpen);
                return false;
            }
            
        }
    }
}